package com.atguigu.algorithm;

public class RabinKarp1 {
    static long hash(String str){
        long hash=0;
        for(int i=0;i<str.length();i++){
            hash=31*hash+str.charAt(i);
        }
        return hash;
    }

    public static void main(String[] args) {
        String s="ABABABA";
        String p="ABA";
        match(p,s);
    }
    public static void match(String s1,String s2){
        //s1为要匹配的字符串
        long hash1=hash(s1);
        int len=s1.length();
        for(int i=0;i+len<=s2.length();i++){
            long hash2=hash(s2.substring(i,i+len));
            if(hash1==hash2){
                System.out.println("match:"+i);
            }
        }
    }
}
